package mlp.game.applebucking;

import java.util.List;
import java.util.Random;

import android.util.Log;

import com.overkill.game.GameEngine;
import com.overkill.game.Sprite;

import mlp.game.applebucking.Apple.Type;

public class RandomAppleGenerator {
	
	private boolean active = true;
	private Random random;
	
	private Sprite[] goodAppleBasket;
	private Sprite[] rottenAppleBasket;
	private Sprite[] powerAppleBasket;
	
	private float chanceOfBadApple;
	private float chanceOfZapApple;
	
	private int maxApples;
	private long DELAY = 1100;
	private long lastTime = 0;
	private String difficulty;	
	
	private int totalDrops = 0;
	private int goodDrops = 0;
	private int rottenDrops = 0;
	private int powerDrops = 0;	
	
	public RandomAppleGenerator(Random random, Sprite[] goodAppleBasket,
			Sprite[] rottenAppleBasket, float chanceOfBadApple, int maxApples) {
		this.random = random;
		this.goodAppleBasket = goodAppleBasket;
		this.rottenAppleBasket = rottenAppleBasket;
		this.chanceOfBadApple = chanceOfBadApple;
		this.maxApples = maxApples;
	}
	
	public RandomAppleGenerator(Random random, float chanceOfBadApple, float chanceOfZapApple, int maxApples, String difficulty) {
		this.random = random;
		this.chanceOfBadApple = chanceOfBadApple;
		this.chanceOfZapApple = chanceOfZapApple;
		this.maxApples = maxApples;
		this.difficulty = difficulty;
	}
	
	public int generate(List<Apple> addTo, int maxRightPosition, long gameTime){
		if(isActive() == false)
			return totalDrops;
		
		if(difficulty.equals("EASY"))
			this.generateEasy(addTo, maxRightPosition, gameTime);
		else if(difficulty.equals("HARD"))
			this.generateHard(addTo, maxRightPosition, gameTime);
		
		return totalDrops;
			
	}
	
	public Apple createApple(Sprite basket[], int maxX, int offsetY, Type type, int speed){
		switch (type) {
		case GOOD:
			goodDrops += 1;
			break;
		case ROTTEN:
			rottenDrops += 1;
			break;
		case POWER:
			powerDrops += 1;
			break;
		}
		totalDrops += 1;
		return new Apple(basket[random.nextInt(basket.length)], maxX, offsetY, type, (int) GameEngine.dip(speed * 1.0f));
	}
	
	private void generateHard(List<Apple> addTo, int maxRightPosition, long gameTime){
		long timePassed = gameTime - lastTime;
		if(timePassed < this.DELAY) return;
		
		int y1 = (int) GameEngine.dip(100f);		
		float r = random.nextFloat();	
		
		if(r > chanceOfBadApple)
			addTo.add(createApple(goodAppleBasket, maxRightPosition, -y1, Type.GOOD, 5));
		else if (r <= chanceOfBadApple && r > chanceOfZapApple)
			addTo.add(createApple(rottenAppleBasket, maxRightPosition, -y1, Type.ROTTEN, 5));
		else
			addTo.add(createApple(powerAppleBasket, maxRightPosition, -y1, Type.POWER, 5));
			
		y1 = random.nextInt((int) GameEngine.dip(200f));
		r = random.nextFloat();		
		
		if(r > chanceOfBadApple)
			addTo.add(createApple(goodAppleBasket, maxRightPosition, -y1, Type.GOOD, 7));
		else if (r <= chanceOfBadApple && r > chanceOfZapApple)
			addTo.add(createApple(rottenAppleBasket, maxRightPosition, -y1, Type.ROTTEN, 7));
		else
			addTo.add(createApple(powerAppleBasket, maxRightPosition, -y1, Type.POWER, 7));
				
		lastTime = gameTime;
		if(this.DELAY >= 700){
			this.DELAY -= 5;
		}		
	}
		
	private void generateEasy(List<Apple> addTo, int maxRightPosition, long gameTime){
		long timePassed = gameTime - lastTime;
		if(timePassed < this.DELAY) return;
		
		int y1 = (int) GameEngine.dip(100f);
		float r = random.nextFloat();	
		
		if(r > chanceOfZapApple)
			addTo.add(createApple(goodAppleBasket, maxRightPosition, -y1, Type.GOOD, 5));
		else
			addTo.add(createApple(powerAppleBasket, maxRightPosition, -y1, Type.POWER, 5));
		
		y1 = random.nextInt((int) GameEngine.dip(200f));
		r = random.nextFloat();	
		
		if(r > chanceOfZapApple)
			addTo.add(createApple(goodAppleBasket, maxRightPosition, -y1, Type.GOOD, 7));
		else
			addTo.add(createApple(powerAppleBasket, maxRightPosition, -y1, Type.POWER, 7));
		
		lastTime = gameTime;
		if(this.DELAY >= 800){
			this.DELAY -= 2;
		}		
	}
	
	public Random getRandom() {
		return random;
	}

	public void setRandom(Random random) {
		this.random = random;
	}

	public Sprite[] getGoodAppleBasket() {
		return goodAppleBasket;
	}

	public void setGoodAppleBasket(Sprite[] goodAppleBasket) {
		this.goodAppleBasket = goodAppleBasket;
	}

	public Sprite[] getRottenAppleBasket() {
		return rottenAppleBasket;
	}
	
	public void setPowerAppleBasket(Sprite[] powerAppleBasket) {
		this.powerAppleBasket = powerAppleBasket;
	}

	public Sprite[] getPowerAppleBasket() {
		return powerAppleBasket;
	}

	public void setRottenAppleBasket(Sprite[] rottenAppleBasket) {
		this.rottenAppleBasket = rottenAppleBasket;
	}

	public float getChanceOfBadApple() {
		return chanceOfBadApple;
	}

	public void setChanceOfBadApple(float chanceOfBadApple) {
		this.chanceOfBadApple = chanceOfBadApple;
	}

	public int getMaxApples() {
		return maxApples;
	}

	public void setMaxApples(int maxApples) {
		this.maxApples = maxApples;
	}

	public void setDifficulty(String difficulty) {
		Log.i("RandomAppleGenerator", "difficulty:" + difficulty);
		this.difficulty = difficulty;
	}

	public String getDifficulty() {
		return difficulty;
	}

	public void setActive(boolean active) {
		this.active = active;
	}

	public boolean isActive() {
		return active;
	}	
	
	public void report(){
		Log.i("RandomAppleGenerator", "total apples: " + totalDrops);
		Log.i("RandomAppleGenerator", "good apples: " + goodDrops + " " + (goodDrops / (1.0f * totalDrops)) + " (chance of " + (1- chanceOfBadApple) + ")");
		Log.i("RandomAppleGenerator", "rotten apples: " + rottenDrops + " " + (rottenDrops / (1.0f * totalDrops)) + " (chance of " + (chanceOfBadApple - chanceOfZapApple)  + ")");
		Log.i("RandomAppleGenerator", "power apples: " + powerDrops + " " + (powerDrops / (1.0f * totalDrops)) + " (chance of " + chanceOfZapApple + ")");
		
	}
}
